package com.example.store.controller;

import com.example.store.common.JsonResult;
import com.example.store.entity.Address;
import com.example.store.entity.BaseEntity;
import com.example.store.service.IAddressService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController extends BaseController {
    @Autowired
    private IAddressService addressService;

    @RequestMapping("add_new_address")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.addNewAddress(uid,username,address);
        return new JsonResult<Void>(OK);
    }
    @GetMapping({"","/"})
    public JsonResult<List<Address>> findByUid_address(HttpSession session){
        Integer uid = getUidFromSession(session);
        List<Address> list = addressService.findAddressByUid(uid);
        return new JsonResult<>(OK,list);
    }

    @RequestMapping("{aid}/set_default")
    public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid, HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.setDefault(aid,uid,username);
        return new JsonResult<Void>(OK);
    }
}
